package com.kgc.warehouse.bussness.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.warehouse.bussness.entity.Goods;
import com.kgc.warehouse.bussness.entity.Inport;
import com.kgc.warehouse.bussness.mapper.GoodsMapper;
import com.kgc.warehouse.bussness.mapper.InportMapper;
import com.kgc.warehouse.bussness.service.InportService;
import com.kgc.warehouse.bussness.vo.InportVO;
import com.kgc.warehouse.system.entity.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
* @author main
* @description 针对表【bus_inport】的数据库操作Service实现
* @createDate 2023-02-04 08:48:32
*/
@Service
@Transactional
public class InportServiceImpl extends ServiceImpl<InportMapper, Inport> implements InportService{

    @Resource
    private InportMapper inportMapper;
    @Resource
    private GoodsMapper goodsMapper;
    @Override
    public void addInport(InportVO inportVO,HttpSession session) {
        //添加进货单
        //设置进货时间
        inportVO.setInporttime(new Date());
        //设置操作人
        User user = (User) session.getAttribute("user");
        inportVO.setOperateperson(user.getName());
        //保存进货数据
        inportMapper.insert(inportVO);

        //修改商品库存
        Goods goods = goodsMapper.selectById(inportVO.getGoodsid());
        goods.setNumber(goods.getNumber()+inportVO.getNumber());
        goodsMapper.updateById(goods);
    }

    @Override
    public void updateInport(InportVO inportVO) {
        //更新商品表
        Inport inport = inportMapper.selectById(inportVO.getId());
        Goods goods = goodsMapper.selectById(inportVO.getGoodsid());
        goods.setNumber(goods.getNumber()-inport.getNumber()+inportVO.getNumber());
        //更新inport表
        inportMapper.updateById(inportVO);
        goodsMapper.updateById(goods);

    }

    @Override
    public void deleteInport(Integer id) {
        Inport inport = inportMapper.selectById(id);
        Goods goods = goodsMapper.selectById(inport.getGoodsid());
        goods.setNumber(goods.getNumber()-inport.getNumber());
        inportMapper.deleteById(id);
        goodsMapper.updateById(goods);
    }


}




